package com.cms.pms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cms.pms.Parser;

public class DefaultParser implements Parser {

	Map<String, Parser> parserMap = new HashMap<String, Parser>();
	List<Parser> parserlist = new ArrayList<Parser>();

	@Override
	public String getTID(String sign, Object[] args) {
		Parser p = this.parserMap.get(sign);
		return p.getTID(sign, args);
	}

	@Override
	public Map<String, String> getTags(String sign, Object[] args) {
		Parser p = this.parserMap.get(sign);
		return p.getTags(sign, args);
	}

	@Override
	public boolean isMatched(String sign) {
		if (parserMap.containsKey(sign)) {
			Parser p = parserMap.get(sign);
			if (p != null)
				return true;
			else
				return false;
		}
		
		for (Parser p1 : parserlist) {
			if (p1.isMatched(sign)) {
				parserMap.put(sign, p1);
				return true;
			}
		}

		parserMap.put(sign, null);
		return false;
	}

	public void addParser(Parser parser) {
		this.parserlist.add(parser);
	}

}
